#ifndef _COMMON_H
#define _COMMON_H

#include <stdio.h>
#include <stdlib.h>

/*求数组元素个数的宏*/
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

/*求结构体成员的偏移*/
#define OFFSETOF(type,member) ((unsigned long)&(((type*)0)->member))

#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a)>(b)?(b):(a))
#define SUM(a,b) ((a)+(b))

/*位运算宏*/
#define SET_BIT_N(a,n)		((a)|(1<<(n)))
#define CLCEAR_BIT_N(a,n)	((a)&~(1<<(n)))
#define REVERT_BIT_(a,n)	((a)^(1<<(n)))
#define GET_BIT_N(a,n)		(((a)&(1<<(n))) != 0)

/*安全释放堆内存的宏*/
#define SAFEFREE(p) do{ \
	if(p){\
		free(p);p=NULL;\
	}\
}while(0)

/*安全关闭文件描述符的宏*/
#define SAFECLOSE(fp) do{\
	if(fp){\
		fclose(fp);fp = NULL;\
	}\
}while(0)

/*自定义的ASSERT宏*/
#define ASSERT(p) do{\
	if(!p){\
		printf("[%s][%s][%d]:Assert Fail!\n",__FILE__,__func__,__LINE__);\
		abort();\
	}\
}while(0)

/*自定义的调试宏*/
#ifdef DEBUG_ON
#define DEBUG(...) do{ \
	printf("[DEBUG][%s][%d]:",__func__,__LINE__);\
	printf(__VA_ARGS__);\
}while(0)
#else
#define DEBUG(...)
#endif

#endif















